package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.ObjectUtils;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.teacher.R;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.qmuiteam.qmui.widget.QMUIEmptyView;

import im.delight.android.webview.AdvancedWebView;

/**
 * @author xubo
 * @date 2018/2/27
 */
@Route(path = RouterHub.APP_WEBVIEWACTIVITY)
public class WebViewActivity extends BaseActivity implements AdvancedWebView.Listener {
    private AdvancedWebView mWebView;
    private QMUIEmptyView mEmptyView;

    @Autowired(name = Constants.ROUTER_STRING)
    String url;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {

    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
        return R.layout.public_activity_webview;
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mWebView = findViewById(R.id.webview);
        mEmptyView = findViewById(R.id.emptyView);
        mEmptyView.show(true);
        mWebView.setListener(this, this);
        mWebView.setGeolocationEnabled(false);
        mWebView.setMixedContentAllowed(true);
        mWebView.setCookiesEnabled(true);
        mWebView.setThirdPartyCookiesEnabled(true);
        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                mEmptyView.hide();
            }

        });
        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                setTitle(title);
            }

        });
        mWebView.addHttpHeader("X-Requested-With", "");
        if (ObjectUtils.isNotEmpty(url)) {
            mWebView.loadUrl(url);
        }
    }

    @SuppressLint("NewApi")
    @Override
    protected void onResume() {
        super.onResume();
        mWebView.onResume();
        // ...
    }

    @SuppressLint("NewApi")
    @Override
    protected void onPause() {
        mWebView.onPause();
        // ...
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mWebView.onDestroy();
        // ...
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        mWebView.onActivityResult(requestCode, resultCode, intent);
    }

    @Override
    public void onBackPressed() {
        if (!mWebView.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void onPageStarted(String url, Bitmap favicon) {
        mEmptyView.show(true);
        mWebView.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onPageFinished(String url) {
        mEmptyView.hide();
        mWebView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onPageError(int errorCode, String description, String failingUrl) {
        if (ObjectUtils.isNotEmpty(mEmptyView)) {
            mEmptyView.show(
                    false,
                    getResources().getString(R.string.public_emptyView_mode_desc_fail_title),
                    null,
                    getResources().getString(R.string.public_emptyView_mode_desc_retry),
                    v -> {
                        mWebView.loadUrl(url);
                    });

        }
    }

    @Override
    public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
    }

    @Override
    public void onExternalPageRequest(String url) {
    }


}
